package com.store.framework.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * <code>FileHandler</code> is a class that define the basic file operation such
 * as read, write, copy, move, delete and so on.
 * 
 * @author Xubin
 */
public class FileHandler {
	/**
	 * 
	 * @param filePath
	 * 				String The absolute path of a file
	 * @return row data by ArrayList<String>
	 */
	public static synchronized ArrayList<String> getSourcedataList(String filePath) {
		if (filePath == null)
			throw new RuntimeException("file path is null");
		ArrayList<String> list = new ArrayList<String>();
		File file = new File(filePath);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF-8"));

			String Ls;
			while ((Ls = br.readLine()) != null) {
				if (Ls == null || Ls.trim().equals("")|| Ls.trim().startsWith("#")) {
					continue;
				}
				list.add(Ls);
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static synchronized ArrayList<String> getSourcedataList(InputStream is) {
		if (is == null)
			throw new RuntimeException("InputStream path is null");
		ArrayList<String> list = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					is, "UTF-8"));

			String Ls;
			while ((Ls = br.readLine()) != null) {
				if (Ls == null || Ls.trim().equals("")|| Ls.trim().startsWith("#")) {
					continue;
				}
				list.add(Ls);
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	/**
	 * 获得某个文件夹中该级的文件的结合
	 * @param is
	 * @return 该文件夹中这级所有文件和文件夹
	 */
	public static synchronized ArrayList<String> getSimpleFileList(String filePath) {
		if (filePath == null)
			throw new RuntimeException("filePath path is null");
		ArrayList<String> list = new ArrayList<String>();
		File file = new File(filePath);
		if(file.isFile()){
			return list;
		}else{
			File []  files = file.listFiles();
			for(File fl : files){
				if(fl.isDirectory()){
					list.add(fl.getName());
				}
			}
			
			for(File fl : files){
				if(fl.isFile()){
					list.add(fl.getName());
				}
			}
			
		}
		return list;
	}

	
	public static synchronized ArrayList<String> getSimpleFileList(	File file) {
		if (file == null)
			throw new RuntimeException("file path is null");
		ArrayList<String> list = new ArrayList<String>();
		if(file.isFile()){
			return list;
		}else{
			File []  files = file.listFiles();
			for(File fl : files){
				if(fl.isFile())
				{
					list.add(fl.getName());
				}else{
					list.add(0,fl.getName());
				}
				
			}				
		}
		return list;
	}
	
	public static synchronized boolean isFile(String path){
		File file = new File(path);
		if(file==null){
			return false;
		}
		return new File(path).isFile();
	}

}